package com.example.medicalapp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;

import com.example.medicalapp.R;
import com.example.medicalapp.adapter.AuthPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class PatientAuthActivity extends AppCompatActivity {
    private static final String TAG = "PatientAuthActivity";
    private TabLayout tabLayout;
    private ViewPager2 viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_auth);
        Log.d(TAG, "onCreate called");

        // 初始化视图
        initViews();

        // 配置ViewPager2适配器
        setupViewPager();
    }

    private void initViews() {
        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);
        Log.d(TAG, "Views initialized");
    }

    private void setupViewPager() {
        // 创建适配器，false表示不是医生注册
        AuthPagerAdapter adapter = new AuthPagerAdapter(this, false);
        viewPager.setAdapter(adapter);

        // 将TabLayout与ViewPager2关联
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("登录");
                    break;
                case 1:
                    tab.setText("注册");
                    break;
            }
        }).attach();

        Log.d(TAG, "ViewPager2 setup completed");
    }

    @Override
    public void onBackPressed() {
        // 如果当前在注册页面，返回登录页面
        if (viewPager.getCurrentItem() == 1) {
            viewPager.setCurrentItem(0);
        } else {
            super.onBackPressed();
            // 应用返回动画
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        }
    }
}
